package com.liaogui.算法题.栈;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * @author liaogui
 * @date 2025/4/10 23:16
 */
public class 有效的括号 {
    public static void main(String[] args) {
        System.out.println(isValid("{()}"));
    }

    public static boolean isValid(String s) {
        Stack<Object> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '{' || c == '[' || c == '(') {
                stack.push(c);
            } else {
                if (stack.isEmpty()) return false;
                char peek = (char)stack.peek();
                if (peek == '[' && c == ']') return false;
                if (peek == '(' && c == ')') return false;
                if (peek == '{' && c == '}') return false;
                stack.pop();
            }
        }
        if (!stack.isEmpty()){
            return false;
        }
        return true;
    }
}
